package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.web.extension.SpaceTypeConverter;
import foo.bar.wiki.domain.Page;
import foo.bar.wiki.domain.Space;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.services.exception.BusinessLogicExecution;
import foo.bar.wiki.utils.WikiUtils;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class AddPageActionBean extends BaseActionBean {

    public static final String VIEW = "/page/addPage.jsp";

    private WikiService wikiService;


    @Validate(converter = PageTypeConverter.class)
    @ValidateNestedProperties({
        @Validate(field = "name", trim = true, required = true, on={"save"}),
        @Validate(field = "description", trim=true, required=true, on={"save"}),
        @Validate(field = "pageLocation", trim = true, required = true, on={"save"}),
        @Validate(field = "space", converter = SpaceTypeConverter.class)
    })
    private Page page;

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution(generateThemePath(VIEW));
    }


    public Resolution save() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                wikiService.savePage(page);
                getContext().getMessages().add(new SimpleMessage("Page ["+page.getName()+"] successfully added to Space ["+page.getSpace().getName()+"]"));
                return new RedirectResolution(PageActionBean.class, "view").addParameter("page", page.getId());
            }
        });
    }
    @ValidationMethod(on={"save"})
    public void validate_save(ValidationErrors validationErrors) {
        if (!WikiUtils.isValidWikiPageOrSpaceName(page.getName())) {
            validationErrors.add("page.name", new SimpleError("Page name should be alphanumeric ( '_' character is allowed) "));
        }
    }



    /*@After(stages={LifecycleStage.BindingAndValidation}, on = {"save"})
    public void before() {
        // when creating a new page, everything is empty, space id in Page object is populated by Stripes
        // we find the Space object if it exists and set it in. ARGGG... ugly code.
        Space space = wikiService.getSpaceById(page.getSpace().getId());
        page.setSpace(space);
    } */



    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }
    

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }


    
}
